﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LoL.classes;

namespace LoL
{
    public enum CollisionType
    {
        collideRight,
        collideLeft,
        collideTop,
        collideBottom,
        none
    }
    /*
     * Klasse for å oppdage potensielle kollisjoner     
     */ 
    class CollisionDetector
    {

        static CollisionDetector()
        {

        }

        public static List<Collision> detectCollision(Actor movingActor)
        {
            List<Collision> collisionList = new List<Collision>();
            Rectangle movingActorRectangle = movingActor.generateBoundingRectangle();
            foreach (Actor actor1 in ActorManager.getListOfActiveActors())
            {
                if (actor1 != movingActor)
                {
                    Rectangle collisionRectangle = actor1.generateBoundingRectangle();
                    if (movingActorRectangle.Intersects(collisionRectangle))
                    {
                        CollisionType direction = checkDirection((Character) movingActor, actor1, movingActorRectangle, collisionRectangle);
                        collisionList.Add(new Collision(direction, actor1));
                    }
                }
            }

            return collisionList;
        }

        private static CollisionType checkDirection(Character movingActor, Actor collidingActor, 
            Rectangle movingActorRectangle, Rectangle collisionRectangle)

        {

            //if (movingActorRectangle.Right >= collisionRectangle.Left
            //    && movingActorRectangle.Right >= collisionRectangle.X
            //    && movingActorRectangle.Right <= collisionRectangle.X + collisionRectangle.Width)
            //{
            //    return CollisionType.collideRight;
            //}

            if (movingActorRectangle.X >= collisionRectangle.X
                && movingActorRectangle.X <= collisionRectangle.X + collisionRectangle.Width
                && movingActorRectangle.Bottom >= collisionRectangle.Bottom
                && movingActorRectangle.Top <= collisionRectangle.Top)
            {
                return CollisionType.collideLeft;
            }

            if (movingActorRectangle.Bottom >= collisionRectangle.Top
                && movingActorRectangle.Bottom >= collisionRectangle.Y
                && movingActorRectangle.Bottom <= collisionRectangle.Y + collisionRectangle.Height
                && movingActor.MovementSpeedY >= 0)
            {
                /* TODO:
                 * actor1 står på actor2.
                 */
                return CollisionType.collideBottom;
            }

            if (movingActorRectangle.Top <= collisionRectangle.Bottom
                && movingActorRectangle.Top >= collisionRectangle.Y
                && movingActorRectangle.Top <= collisionRectangle.Y + collisionRectangle.Height
                && movingActor.MovementSpeedY < 0)
            {
                /* TODO:
                 * actor1 skaller i actor2. 
                 */
                return CollisionType.collideTop;
            }

            
            return CollisionType.none;

        }

    }
}
